from utils import ave_util, k_util, json_util


# x = {'key': rq, 'value': y} x日期从小到大
def parse_x(ks, key, value):
    x = []
    for k in ks:
        k = json_util.to_json(k)
        x.append({'key': k[key], 'value': float(k[value])})
    return x


def MA(code, size):
    return ave_util.ave_line_price_n(code, k_util.get_day_k(code), size)


def MAK(code, size, klines):
    return ave_util.local_ave_line(code, size, 1, klines)


def DRAWTEXT(flag, a, b):
    print('DRAWTEXT')


# n天前的值
def REF(x, n):
    len_num = len(x)
    ref_list = []
    for i in range(n):
        ref_list.append({'key': x[i]['key'], 'value': 0})
    for i in range(n, len_num):
        ref_list.append({'key': x[i]['key'], 'value': x[i - n]['value']})
    return ref_list


# 表示当A从下方向上穿过B时返回1,否则返回0
# a:list
# b:list
# 返回参数上传点和标识
def CROSS(a, b):
    CROSS_list = []
    # 数据验证
    av0 = json_util.to_json(a[0])
    bv0 = json_util.to_json(b[0])
    if av0['key'] != bv0['key'] or len(a) != len(b):
        print('表示当A从下方向上穿过B时返回1,否则返回0：数据格式错误')
        return '数据格式错误'
    if av0['value'] < bv0['value']:
        flag = -1
    else:
        flag = 1
    for i in range(len(a)):
        avi = json_util.to_json(a[i])
        bvi = json_util.to_json(b[i])
        if avi['value'] >= bvi['value'] and flag == -1:
            CROSS_list.append({'key': avi['key'], 'value': 1})
            flag = 1
            continue
        elif avi['value'] < bvi['value'] and flag == 1:
            flag = -1
        CROSS_list.append({'key': avi['key'], 'value': 0})
    return CROSS_list


# 同花顺 LLV
# x = {'key': rq, 'value': y} x日期从小到大
# 1 2 3 4 5 6 7 8 9 10 11 12 13
def LLV(x, n):
    print('llv')
    llv_list = []
    llv_list.append(x[0])
    min_num = 0
    min_value = x[0]['value']
    for i in range(1, len(x)):
        a = json_util.to_json(x[i])
        size = min(i + 1, n)
        if (i - size) == min_num:
            min_value = a['value']
            min_num = i
            for j in range(1, size):
                b = json_util.to_json(x[i - j])
                if min_value > b['value']:
                    min_value = b['value']
                    min_num = i - j
        else:
            if min_value > a['value']:
                min_value = a['value']
                min_num = i
        llv_list.append({'key': a['key'], 'value': min_value})
    return llv_list


# 同花顺 HHV
def HHV(x, n):
    print('HHV')
    hhv_list = []
    hhv_list.append(x[0])
    max_num = 0
    max_value = x[0]['value']
    for i in range(1, len(x)):
        a = json_util.to_json(x[i])
        size = min(i + 1, n)
        if (i - size) == max_num:
            max_value = a['value']
            max_num = i
            for j in range(1, size):
                b = json_util.to_json(x[i - j])
                if max_value < b['value']:
                    max_value = b['value']
                    max_num = i - j
        else:
            if max_value < a['value']:
                max_value = a['value']
                max_num = i
        hhv_list.append({'key': a['key'], 'value': max_value})
    return hhv_list


# 同花顺 SMA
# n 天的平滑曲线
# m 权重
def SMA(x, n, m):
    print('SMA')
    sma_list = []
    x0 = json_util.to_json(x[0])
    y = float(x0['value'])
    rq0 = x0['key']
    sma_info = {'key': rq0, 'value': y}
    sma_list.append(sma_info)
    daks_len = len(x)
    for i in range(1, daks_len):
        xi = json_util.to_json(x[i])
        sp = float(xi['value'])
        rq = xi['key']
        y = y + (sp - y) * m / n
        sma_list.append({'key': rq, 'value': y})
    # sma_list 日期从小到大
    # Y = [mX + (n - m)Y"]/n
    return sma_list


# 同花顺 EMA
def EMA(x, n):
    print('EMA')
    ema_list = []
    x0 = json_util.to_json(x[0])
    y = float(x0['value'])
    rq0 = x0['key']
    ema_info = {'key': rq0, 'value': y}
    ema_list.append(ema_info)
    daks_len = len(x)
    for i in range(1, daks_len):
        xi = json_util.to_json(x[i])
        sp = float(xi['value'])
        rq = xi['key']
        y = y + (sp - y) * 2 / n
        # EMA12 = EMA12 + (sp - EMA12) * 2 / 13
        ema_list.append({'key': rq, 'value': y})
    # ［2 * X + (N - 1) * Y’］ / (N + 1)
    return ema_list





if __name__ == '__main__':
    ks = k_util.day_k_local('002959', 10)
    x = parse_x(ks, 'rq', 'min')
    llv_list = HHV(x, 2)
    ks_list = []
    llv = []
    for i in range(10):
        ks_list.append(float(ks[i]['min']))
        llv.append(llv_list[i]['value'])
    print(ks_list)
    print(llv)
    print('')
